package com.hsgene.product.controller;

import com.hsgene.common.RangeType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.product.condition.CommodityCategoryCondition;
import com.hsgene.product.domain.GeneticCommodityCategory;
import com.hsgene.product.exception.ProductErrorCode;
import com.hsgene.product.service.CommodityCategoryService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商品类目
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/commodities/category")
public class CommodityCategoryController {
    @Autowired
    CommodityCategoryService commodityCategoryService;

    /**
     * 添加商品类目
     *
     * @param commodityCategory
     * @return
     */
    @PostMapping
    public ApiResult addCategory(@RequestBody GeneticCommodityCategory commodityCategory) {
        return commodityCategoryService.addCategory(commodityCategory);
    }

    /**
     * 修改商品类目
     *
     * @param commodityCategory
     * @return
     */
    @PutMapping
    public ApiResult updateCategory(@RequestBody GeneticCommodityCategory commodityCategory) {
        return commodityCategoryService.updateCategory(commodityCategory);
    }

    /**
     * 根据id查找类目
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ApiResult<GeneticCommodityCategory> getById(@PathVariable("id") String id) {
        return commodityCategoryService.getById(id);
    }

    /**
     * 类目列表
     * @param orderBy
     * @param isAsc
     * @param page
     * @param size
     * @return
     */
    @GetMapping
    public ApiResult queryCategory(
            @RequestParam(name = "orderBy", required = false) String orderBy,
            @RequestParam(name = "isAsc", defaultValue = "false") Boolean isAsc,
            @RequestParam(name = "page", defaultValue = "1") int page,
            @RequestParam(name = "size", defaultValue = "10") int size
            ,@RequestParam(name="category",required = false)String category
            ,@RequestParam(name="range_type",required = false) String rangeType
            ) {
        //获取全部类目
        if(RangeType.ALL.getMessage().equals(rangeType)){
            return commodityCategoryService.getAll();
        }else{
            //获取类目分页列表
            CommodityCategoryCondition condition = new CommodityCategoryCondition();
            condition.setCategory(category);
            condition.setLimitHelper(LimitHelper.create(page, size));
            if (!StringUtils.isEmpty(orderBy)) {
                condition.setOrderHelper(OrderHelper.create(orderBy, isAsc));
            }
            return commodityCategoryService.queryCategory(condition);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ApiResult deletecory(@PathVariable("id") String id) {
        return commodityCategoryService.deletecory(id);
    }



    /**
     *
     * @Description 商品类目是否重复
     * @Author FX
     * @CreateDate 2018/10/16 18:16
     * @param id
     * @param category
     * @return
     */
    @GetMapping("name")
    public ApiResult judgeCategoryName(String id,String category){
        ApiResult byName = commodityCategoryService.getByName(category);
        if(StringUtils.isNotEmpty(id)){
            ApiResult<GeneticCommodityCategory> byId = commodityCategoryService.getById(id);
            if(byId.getData().getCategory().equals(category)){
                return ApiResult.succ();
            }
        }
        if(byName.getData()==null){
            return ApiResult.succ();
        }
        return ApiResult.fail(ProductErrorCode.EXISTED) ;
    }


}
